package com.example.controller;

import com.example.model.BlogInfo;
import com.example.model.Result;
import com.example.service.Impl.BlogServiceImpl;
import com.example.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Description:
 * User: 阿宾
 * Date: 2025-05-14
 * Time: 19:09
 */
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogServiceImpl blogService;

    /**
     * 查询博客列表
     * @return
     */
    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        return blogService.getList();
    }

    /**
     * 通过博客Id,查询博客详情
     * @param blogId
     * @return
     */
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        //1.判断博客作者是否是登录作者
        //2.从token中获取用户id
        //2.1.从请求的header中获取token
        String token = request.getHeader("user_token_header");
        //2.2.根据token获取登录用户ID(肯定可以获取，因为已经登录了)
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == blogDetail.getUserId()){
            blogDetail.setIsLoginUser(true);
        }else {
            blogDetail.setIsLoginUser(false);
        }
        return blogDetail;
    }

    /**
     * 添加博客
     *
     * @param blogInfo
     * @return
     */
    @RequestMapping("/add")
    public Result addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request){
        //1.参数校验
        if(!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent()))
            return Result.fail("博客标题或内容不能为空");

        //2.从token中获取用户id
        //2.1.从请求的header中获取token
        String token = request.getHeader("user_token_header");
        //2.2.根据token获取登录用户ID(肯定可以获取，因为已经登录了)
        Integer userId = JwtUtils.getIdByToken(token);

        //3.添加用户ID
        blogInfo.setUserId(userId);

        //4.添加博客
        Boolean result = blogService.addBlog(blogInfo);

        //5.结果校验
        if(!result){
            log.info("博客添加失败，result：{}",result);
            return Result.fail("添加博客失败");
        }
        return Result.success(result);
    }

    /**
     * 删除博客
     * 实现逻辑删除
     * @param blogId
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer blogId){
        //1.参数校验
        if(blogId <= 0){
            Result.fail("博客不存在");
        }
        //2.设置参数
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);//表示逻辑删除

        Boolean result = blogService.deleteBlog(blogInfo);
        //3.结果校验
        if(!result){
            log.info("博客删除失败，result：{}",result);
            return Result.fail("删除博客失败");
        }
        return Result.success(result);
    }

    /**
     * 更新博客
     * @param blogInfo
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody BlogInfo blogInfo){
        log.info("更新博客：{}",blogInfo);
        //1.参数校验

        //2.调用方法
        Boolean result = blogService.updateBlog(blogInfo);
        //3.结果校验
        if(!result){
            log.info("博客更新失败，result：{}",result);
            return Result.fail("更新博客失败");
        }
        return Result.success(result);
    }

}
